package com.hcy.app.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.hcy.app.LocalNavController
import com.hcy.app.R
import com.hcy.app.screen.cap.Cap_Desk
import com.hcy.app.screen.cap.Cap_Home
import com.hcy.app.screen.cap.Cap_Setting
import com.hcy.app.screen.cipher.CipherViewModel
import com.hcy.app.screen.cipher.Contact_Page
import com.hcy.app.screen.cipher.Home_Page
import com.hcy.app.screen.cipher.Note_Edit
import com.hcy.app.screen.cipher.Secret_Form
import com.hcy.app.screen.cipher.Secret_Key_Page
import com.hcy.app.screen.miao.Idp_Home
import com.hcy.app.screen.music.components.File_Upload
import com.hcy.app.screen.music.components.MUSIC_PLAY
import com.hcy.app.screen.music.components.Song_List_Screen
import com.hcy.app.screen.nfc.NFC_Page
import com.hcy.app.screen.relax.Book_Page
import com.hcy.app.screen.relax.Relax_Home
import com.hcy.app.screen.relax.Video_Page

@Composable
fun AppNavHost() {
    val navController = LocalNavController.current
    NavHost(navController = navController,startDestination = "home") {
        composable(route = "home") { Miao_Home() }
        // 音乐播放器页面
        composable(route = "musicPlayer") { MUSIC_PLAY() }
        composable(route = "musicList") { Song_List_Screen() }
        composable(route = "musicUpload") { File_Upload() }
        // 阅读器页面
        composable(route = "relax") { Relax_Home() }
        composable(route = "relax/book") { Book_Page() }
        composable(route = "relax/video") { Video_Page() }
        // cap 页面
        composable(route = "cap/home") { Cap_Home() }
        composable(route = "cap/setting") { Cap_Setting() }
        composable(
            route = "cap/desk/{linkId}/{sessionId}",
            arguments = listOf(
            navArgument("linkId") { type = NavType.StringType },
            navArgument("sessionId") { type = NavType.StringType })
        ) { backStackEntry ->
            // 从 backStackEntry 中提取参数
            val linkId = backStackEntry.arguments?.getString("linkId")!!
            val sessionId = backStackEntry.arguments?.getString("sessionId")!!
            Cap_Desk(linkId=linkId, sessionId = sessionId)
        }
        // 密信页面
        composable(route = "cipher/home") { Home_Page() }
        composable(
            route = "cipher/edit/{noteId}",
            arguments = listOf(
                navArgument("noteId") { type = NavType.LongType  }
            )
        ) { backStackEntry ->
            val noteId = backStackEntry.arguments?.getLong("noteId")!!
            Note_Edit(noteId = noteId)
        }
        composable(route = "cipher/contact") { Contact_Page() }
        composable(route = "cipher/contactForm") { Contact_Page() }// todo 还没写
        composable(route = "cipher/secret") { Secret_Key_Page() }
        composable(
            route = "cipher/secretForm/{keyId}",
            arguments = listOf(
                navArgument("keyId") { type = NavType.LongType  }
            )
        ) { backStackEntry ->
            val keyId = backStackEntry.arguments?.getLong("keyId")!!
            Secret_Form(keyId = keyId)
        }



        // 测试页面
        composable(route = "nfcTest") { NFC_Page() }
        composable(route = "test") { Cap_Home() }
        composable(route = "idp") { Idp_Home() }
    }
}

@Preview
@Composable
fun PreviewHomeScreen() {
    val navController = rememberNavController()
    CompositionLocalProvider(LocalNavController provides navController) {
        Miao_Home()
    }
}

/**
 * 像此类private权限限制的compose的UI，命名可以不用_下划线风格，因为不太会在其他地方出现干扰
 */
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun Miao_Home(){
    Column(
        modifier = Modifier.fillMaxSize()
            .systemBarsPadding().background(Color(0xFFE8E096)),
    ) {
        Text(
            text = "Miao App", modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            fontSize = 30.sp,
            fontStyle = FontStyle.Italic,
            fontFamily = FontFamily.Serif,
            textAlign = TextAlign.Center
        )
        Text(
            text = "miao 是 hcy 开发的 Android 工具集，包含常用功能和示例代码。具体包括：" +
                    "\n 音乐：本地离线音乐播放器 " +
                    "\n 娱乐：本地离线电纸书阅读器和视频播放器" +
                    "\n 云应用：基于web的vnc、rdp、sh协议调用" +
                    "\n 密信：一个在用户遭遇意外时，自动将其加密的敏感信息发送给指定人，并通过密钥解密的模块",
            color = Color.Gray,
            fontSize = 14.sp,
            fontStyle = FontStyle.Italic,
            fontFamily = FontFamily.SansSerif,
            modifier = Modifier.padding(start = 5.dp, end = 5.dp)
        )
        Text(
            text = "idp：原Android信息采集和RFID demo,已废弃！" ,
            color = Color.Gray,
            fontSize = 14.sp,
            fontStyle = FontStyle.Italic,
            fontFamily = FontFamily.SansSerif,
            modifier = Modifier.padding(start = 5.dp, end = 5.dp),
            style = TextStyle(
                textDecoration = TextDecoration.LineThrough // 添加删除线
            )
        )
        FlowRow{
            var moduleList = listOf<ModuleInfo>(
                ModuleInfo("Music","musicPlayer",R.drawable.hcy_mv1,"本地离线音乐播放器"),
                ModuleInfo("Relax","relax",R.drawable.hcy_mv2,"本地离线电纸书阅读器和视频播放器"),
                ModuleInfo("Link","cap/home",R.drawable.hcy_mv1,"基于ws的vnc、rdp、sh协议调用"),
                ModuleInfo("Cipher","cipher/home",R.drawable.hcy_mv1,"特定场景下的信息加密和定时发送模块"),
                ModuleInfo("IDP","idp",R.drawable.hcy_mv1,"原Android信息采集和RFID demo"),
                ModuleInfo("Test","test",R.drawable.hcy_mv1,"测试功能"),
            )
            moduleList.forEach { it->
                Module_Item( it.title,it.context,it.pngId,it.routerUri )
            }
        }
    }
}

@Composable
private fun Module_Item(title: String,context: String,pngId: Int,routerUri: String){
    val navController = LocalNavController.current
    Card(
        modifier= Modifier.width((LocalConfiguration.current.screenWidthDp.dp/2))
            .padding(start = 5.dp, bottom = 5.dp, end = 5.dp)
            .clickable{
                navController.navigate(routerUri)
            },
    ) {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            ) {
            Image(
                contentScale = ContentScale.Crop, // 裁剪多余部分，保持比例
                painter = painterResource(id= pngId),
                contentDescription = null,
                modifier = Modifier.height(150.dp)
                    .width((LocalConfiguration.current.screenWidthDp.dp/4))
            )
            Column(
                modifier = Modifier.height(150.dp)
                    .width((LocalConfiguration.current.screenWidthDp.dp/4)),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Text(
                    modifier = Modifier.padding(10.dp),
                    text = title,
                    style = TextStyle(
                        fontWeight = FontWeight.Bold
                    )
                )
                Text(
                    text = context,
                    modifier = Modifier.padding(3.dp),
                )
            }
        }
    }
}

data class ModuleInfo(
    val title: String,
    val routerUri: String,
    val pngId: Int,
    val context: String,
)
